program mge2gp

	use mpsge
	use mpsGP
	use mge2gms

        implicit none

        character (len=255) :: fname, fnameroot
	integer n1   ! KP
	logical :: InputFileExists, openok

	character (len=1024) :: CMDLine ! KP

	nullify(gmsrecords)

! Initialize all error message strings  ! KP
	call InitializeMessages('all',0) ! KP

!!      Read file name and model name from the command line:

        call getcl(CMDLine)
	call ProcessCommandLine(CMDLine)

! Temporary
!	fnameroot='basic'
!	fnameroot='demo'
!	fnameroot='demo2'
! Now expect InputFileName on command line - later may prompt for it if not there.
	if(InputFileName.ne.' ') then
	  n1 = INDEX(InputFileName,'.',back=.true.)
	  if(n1.gt.1) then
	    fnameroot = InputFileName(1:n1-1)
	  else
	    fnameroot = InputFileName
	  end if
	else
	  call InitializeMessages('all',0)
	  
	  call ErrorStop('Expect input file name to be on the command line.')
	end if
	  

! KP Set values to tell what conversions want to do
! Now set from the command line
!NO	ConvertToGAMS=.false.
!NO	ConvertToGEMPACK=.true.

!        open(iomps,file='basic.mps')
!        open(iomps,file=trim(fnameroot)//'.mps')
	inquire(file=InputFileName, Exist=InputFileExists)

	if(.not.InputFileExists) then
	  call InitializeMessages('all',0)
	  call ErrorStop('Unable to open Input MPSGE file - it does not exist')
	end if

	openok=.false.
        open(iomps,file=trim(InputFileName),status='old',err=18)
	openok=.true.
18	continue
	if(.not.openok) then
	  call InitializeMessages('all',0)
	  call ErrorStop('Unable to open Input MPSGE file')
	end if

	if(ConvertToGAMS) then   ! KP
!          open(iogms,file='basic_mps.gms')
! Delete IOGMS file if it exists
	  fname=trim(fnameroot)//'_mge.gms'
	  openok=.false.
          open(iogms,file=fname,status='old', err=22)  ! KP
	  openok=.true.
22	  continue
	  if(openok) then
	    close(unit=iotabgp, status='delete')
	  end if
! Open new file on IOGMS
	  openok=.false.
	  open(iogms,file=fname,status='new',err=23)
	  openok=.true.
23	  continue
	  if(.not.openok) then
	    call InitializeMessages('all',0)
	    call ErrorStop('Unable to open new GMS file')
	  end if
	end if

!--------------------------------------------------
! KP to get conversion to GEMPACK started
	if(ConvertToGEMPACK) then   ! KP

! Delete IOTABGP file if it exists
 	  tabfileroot=trim(fnameroot)//'_mge'  
	  fname=trim(tabfileroot)//'.tab'
	  openok=.false.
	  open(iotabgp,file=fname,status='old', err=24)
	  openok=.true.
24	  continue
	  if(openok) then
	    close(unit=iotabgp, status='delete')
	  end if
! Open new file on IOTABGP
	  openok=.false.
	  open(iotabgp,file=fname,status='new',err=26)
	  openok=.true.
26	  continue
	  if(.not.openok) then
	    call InitializeMessages('all',0)
	    call ErrorStop('Unable to open new TAB file')
	  end if
	  tabfilename=fname

! Delete IOCMFGP file if it exists  
	  fname=trim(tabfileroot)//'.cmf'
	  openok=.false.
	  open(iocmfgp,file=fname,status='old', err=28)
	  openok=.true.
28	  continue
	  if(openok) then
	    close(unit=iocmfgp, status='delete')
	  end if
! Open new file on IOCMFGP
	  openok=.false.
	  open(iocmfgp,file=fname,status='new',err=32)
	  openok=.true.
32	  continue
	  if(.not.openok) then
	    call InitializeMessages('all',0)
	    call ErrorStop('Unable to open new Command file')
	  end if
	  cmffilename=fname

!	 End "if(ConvertToGEMPACK) then"
	end if
!--------------------------------------------------

!      Master routine to read an MPSGE input file:
!        - Read one line at a time into a linked list for listing.
!        - Locate functions and set up pointers to the constituent records.
!        - Parse records into fields.
!
        call readmps

!      Parse all fields in the file into constituent elements:
!      - Field identifier text
!      - Identifier argument text
!      - Spanning domain text
!      - Condition text.

        call parse

!       Read variables:

        call readvar

!       Add sector ids to the symbol table and verify that
!       multiple prod blocks have not be included.

        call sectors

!       Check input syntax on prod: records:

        call chkprod

!       Check input syntax on demand: records:

        call chkdemand

!       Read the nesting assignments from the $prod line:

        call readnest

!       Read production function coefficients and set up list of
!       nest elements:

        call readcoef

!--------------------------------------------------
	if(ConvertToGEMPACK) then

!  Temporary
	  call sectors

! Set up GEMPACK names
	  call SetupGEMPACKNames

!	KP. Convert to GEMPACK
!	Writes TAB file and CMF file

	  call ConvertToGP

!	 End "if(ConvertToGEMPACK) then"
	end if
!--------------------------------------------------

!--------------------------------------------------
	if(ConvertToGAMS) then    ! added by KP

!         Output some tests of the input data:

          call testsym

!         Generate indices for all the nests and netputs:

          call indices

          call functions

          call buildmodel

	  call writemodel

!	 End "if(ConvertToGAMS) then"
	end if
!--------------------------------------------------

!       Generate an echoprint of the input file and the functions:

!.      call echoprint
!.      call fnecho

! KP. This ends the main program


        end program mge2gp
